<?php

namespace app\system\controller;
use app\common\base\_Db;
use app\common\controller\CommonController;
use app\common\entity\RequestEntity;
use think\Db;
use app\common\controller\Secret;

class Busdriver extends CommonController{
    public function Listdriver()
    {
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        $where = '1=1';
        if(!empty($get_keyword->text)){
            $where .= ' and i.bus_number = '.$get_keyword->text;
        }
        _Db::Validates($request_data);
        $param_page = $get_page = input('Page', 1);
        $param_pagesize = 10;
        $res = Db::name('bus_driver')
            ->alias('b')
            ->field(
                [
                    'staff_id'=>'StaffId',
                    'i.bus_number'=>'BusNumber',
                    'oper_time'=>'OperTime'
                ]
            )
            ->join('bus_info i','b.deviceid = i.vehicle_device_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->select();
        foreach ( $res as $key => $value){
            if($res[$key]['StaffId']){
                $res[$key]['StaffId']=$this->GetUserName($res[$key]['StaffId']);
            }
            if($res[$key]['OperTime']){
                $res[$key]['OperTime'] = date('Y-m-d H:i:s',$res[$key]['OperTime']);
            }
            $res[$key]['Order'] = $key + 1;
        }
        $int_total_count = count(Db::name('bus_driver b')
            ->join('bus_info i', 'b.deviceid = i.vehicle_device_id')
            ->select());
        $data['tableData']=$res;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    public function GetUserName($id){
        $res = Db::name('staff')
            ->field('nick_name')
            ->where('staff_id ='.$id)
            ->find();
        return $res['nick_name'];
    }
//    public function test(){
//        $arr = [];
//        $res = [];
//        for($i = 1;$i<=2092;$i++){
//            $secret = new Secret();
//            $arr = ['secret'=>$secret->Secret];
//            Db::name('external_staff')->where('id='.$i)->update($arr);
//            $secret = Null;
//        }
//        echo "更新成功";
//    }
}